// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.geniusai.aip.auth.config.ExternalConfig;
import com.geniusai.aip.auth.domain.ResourceCustomVGpuDO;
import com.geniusai.aip.auth.domain.ResourceCustomVGpuRequest;
import com.geniusai.aip.auth.exception.AuthLoginException;
import com.geniusai.aip.auth.exception.AuthLoginExceptionEnum;
import com.geniusai.aip.auth.mapper.ResourceCustomVGpuMapper;
import com.geniusai.aip.auth.service.ResourceCustomVGpuService;
import com.geniusai.aip.feature.common.utils.json.JacksonUtils;
import com.geniusai.aip.feature.common.web.utils.UrlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lingchuanzhou19320
 * @date 2022/8/15
 */
@Service
@Slf4j
public class ResouceCustomVGpuServiceImpl implements ResourceCustomVGpuService {

    @Autowired
    private ExternalConfig externalConfig;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    ResourceCustomVGpuMapper resourceCustomVGpuMapper;

    @Override
    public void customVGpuMemorySize() {

        List<ResourceCustomVGpuRequest> resourceCustomVGpuRequests = new ArrayList<>();

        List<ResourceCustomVGpuDO> resourceCustomVGpuDOS = resourceCustomVGpuMapper.selectList(new QueryWrapper<>());
        Map<String, List<ResourceCustomVGpuDO>> vgpuDisplayNameResources = resourceCustomVGpuDOS.stream().collect(Collectors.groupingBy(ResourceCustomVGpuDO::getVgpuDisplayName));
        for (ResourceCustomVGpuDO resourceCustomVGpuDO: resourceCustomVGpuDOS) {
            ResourceCustomVGpuRequest resourceCustomVGpuRequest = new ResourceCustomVGpuRequest();
            resourceCustomVGpuRequest.setName(resourceCustomVGpuDO.getVgpuDisplayName());
            resourceCustomVGpuRequest.setDisplayName(resourceCustomVGpuDO.getVgpuDisplayName());
            resourceCustomVGpuRequest.setMemory(resourceCustomVGpuDO.getVgpuMemory() * 900);
            List<ResourceCustomVGpuRequest.Device> devices = new ArrayList<>();
            List<ResourceCustomVGpuDO> resourceCustomVGpuDOList = vgpuDisplayNameResources.get(resourceCustomVGpuDO.getVgpuDisplayName());
            for (ResourceCustomVGpuDO resourceCustomVGpuDevice: resourceCustomVGpuDOList) {
                ResourceCustomVGpuRequest.Device device = new ResourceCustomVGpuRequest.Device();
                device.setDeviceName(resourceCustomVGpuDevice.getVgpuDeviceName());
                device.setRatio(resourceCustomVGpuDevice.getVgpuRatio());
                devices.add(device);
            }
            resourceCustomVGpuRequest.setDevices(devices);
            resourceCustomVGpuRequests.add(resourceCustomVGpuRequest);
            try {
                String result = restTemplate.postForObject(
                        UrlUtils.createUrl(
                                externalConfig.getOrionControllerService(),
                                externalConfig.getOrionControllerPort() + "", "v2/custom_devices"
                        ), resourceCustomVGpuRequest, String.class
                );
                log.info("自定义vGpu结果：" + result);
            } catch (Exception e) {
                throw new AuthLoginException(AuthLoginExceptionEnum.CUSTOM_VGPU_ERROR, e);
            }

        }


    }

}
